﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Analysis_Sorting_Algorithms
{
    class InsertionSort
    {
       public int[] sortIt(int[] array, ref int compare)
        {
            int j = 0; //iterator for inner loop
            int key = 0;    //holds value of array element to insert

            for (int i = 1; i < array.Length ; i++) //starting at each element
            {
                j = i - 1;
                key = array[i];     // save current element for swap
                while (j >= 0 && array[j] > key)    //still inside array and larger value not encountered
                {
                    compare++;
                    array[j + 1] = array[j];    //shift current value foward
                    j--;
                }
                array[j + 1] = key; //reinsert pulled value
                compare ++; //when the while loop failed
            }
            return array;
        }
    }
}
